<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="题库ID" prop="bankId">
        <el-input
          v-model="queryParams.bankId"
          placeholder="请输入题库ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="当前题目ID" prop="currentQuestionId">
        <el-input
          v-model="queryParams.currentQuestionId"
          placeholder="请输入当前题目ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="当前题目序号" prop="currentSortOrder">
        <el-input
          v-model="queryParams.currentSortOrder"
          placeholder="请输入当前题目序号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="题库总题数" prop="totalQuestions">
        <el-input
          v-model="queryParams.totalQuestions"
          placeholder="请输入题库总题数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="已完成题数" prop="completedQuestions">
        <el-input
          v-model="queryParams.completedQuestions"
          placeholder="请输入已完成题数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="答对题数" prop="correctQuestions">
        <el-input
          v-model="queryParams.correctQuestions"
          placeholder="请输入答对题数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="完成百分比" prop="progressPercent">
        <el-input
          v-model="queryParams.progressPercent"
          placeholder="请输入完成百分比"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="正确率" prop="accuracyRate">
        <el-input
          v-model="queryParams.accuracyRate"
          placeholder="请输入正确率"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最后练习时间" prop="lastPracticeTime">
        <el-date-picker clearable
          v-model="queryParams.lastPracticeTime"
          type="date"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="请选择最后练习时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="总练习时长(秒)" prop="totalPracticeTime">
        <el-input
          v-model="queryParams.totalPracticeTime"
          placeholder="请输入总练习时长(秒)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否完成(0未完成 1已完成)" prop="isCompleted">
        <el-input
          v-model="queryParams.isCompleted"
          placeholder="请输入是否完成(0未完成 1已完成)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['exam:userBankProgress:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['exam:userBankProgress:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['exam:userBankProgress:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['exam:userBankProgress:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userBankProgressList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="进度ID" align="center" prop="progressId" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="题库ID" align="center" prop="bankId" />
      <el-table-column label="当前题目ID" align="center" prop="currentQuestionId" />
      <el-table-column label="当前题目序号" align="center" prop="currentSortOrder" />
      <el-table-column label="题库总题数" align="center" prop="totalQuestions" />
      <el-table-column label="已完成题数" align="center" prop="completedQuestions" />
      <el-table-column label="答对题数" align="center" prop="correctQuestions" />
      <el-table-column label="完成百分比" align="center" prop="progressPercent" />
      <el-table-column label="正确率" align="center" prop="accuracyRate" />
      <el-table-column label="最后练习时间" align="center" prop="lastPracticeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastPracticeTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="总练习时长(秒)" align="center" prop="totalPracticeTime" />
      <el-table-column label="是否完成(0未完成 1已完成)" align="center" prop="isCompleted" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['exam:userBankProgress:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['exam:userBankProgress:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户题库进度对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="题库ID" prop="bankId">
          <el-input v-model="form.bankId" placeholder="请输入题库ID" />
        </el-form-item>
        <el-form-item label="当前题目ID" prop="currentQuestionId">
          <el-input v-model="form.currentQuestionId" placeholder="请输入当前题目ID" />
        </el-form-item>
        <el-form-item label="当前题目序号" prop="currentSortOrder">
          <el-input v-model="form.currentSortOrder" placeholder="请输入当前题目序号" />
        </el-form-item>
        <el-form-item label="题库总题数" prop="totalQuestions">
          <el-input v-model="form.totalQuestions" placeholder="请输入题库总题数" />
        </el-form-item>
        <el-form-item label="已完成题数" prop="completedQuestions">
          <el-input v-model="form.completedQuestions" placeholder="请输入已完成题数" />
        </el-form-item>
        <el-form-item label="答对题数" prop="correctQuestions">
          <el-input v-model="form.correctQuestions" placeholder="请输入答对题数" />
        </el-form-item>
        <el-form-item label="完成百分比" prop="progressPercent">
          <el-input v-model="form.progressPercent" placeholder="请输入完成百分比" />
        </el-form-item>
        <el-form-item label="正确率" prop="accuracyRate">
          <el-input v-model="form.accuracyRate" placeholder="请输入正确率" />
        </el-form-item>
        <el-form-item label="最后练习时间" prop="lastPracticeTime">
          <el-date-picker clearable
            v-model="form.lastPracticeTime"
            type="date"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择最后练习时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="总练习时长(秒)" prop="totalPracticeTime">
          <el-input v-model="form.totalPracticeTime" placeholder="请输入总练习时长(秒)" />
        </el-form-item>
        <el-form-item label="是否完成(0未完成 1已完成)" prop="isCompleted">
          <el-input v-model="form.isCompleted" placeholder="请输入是否完成(0未完成 1已完成)" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUserBankProgress, getUserBankProgress, delUserBankProgress, addUserBankProgress, updateUserBankProgress } from "@/api/exam/userBankProgress";

export default {
  name: "UserBankProgress",
  data() {
    return {
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户题库进度表格数据
      userBankProgressList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        bankId: null,
        currentQuestionId: null,
        currentSortOrder: null,
        totalQuestions: null,
        completedQuestions: null,
        correctQuestions: null,
        progressPercent: null,
        accuracyRate: null,
        lastPracticeTime: null,
        totalPracticeTime: null,
        isCompleted: null,
        remark: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        progressId: [
          { required: true, message: "进度ID不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
        bankId: [
          { required: true, message: "题库ID不能为空", trigger: "blur" }
        ],
        currentQuestionId: [
          { required: true, message: "当前题目ID不能为空", trigger: "blur" }
        ],
        currentSortOrder: [
          { required: true, message: "当前题目序号不能为空", trigger: "blur" }
        ],
        totalQuestions: [
          { required: true, message: "题库总题数不能为空", trigger: "blur" }
        ],
        completedQuestions: [
          { required: true, message: "已完成题数不能为空", trigger: "blur" }
        ],
        correctQuestions: [
          { required: true, message: "答对题数不能为空", trigger: "blur" }
        ],
        progressPercent: [
          { required: true, message: "完成百分比不能为空", trigger: "blur" }
        ],
        accuracyRate: [
          { required: true, message: "正确率不能为空", trigger: "blur" }
        ],
        lastPracticeTime: [
          { required: true, message: "最后练习时间不能为空", trigger: "blur" }
        ],
        totalPracticeTime: [
          { required: true, message: "总练习时长(秒)不能为空", trigger: "blur" }
        ],
        isCompleted: [
          { required: true, message: "是否完成(0未完成 1已完成)不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户题库进度列表 */
    getList() {
      this.loading = true;
      listUserBankProgress(this.queryParams).then(response => {
        this.userBankProgressList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        progressId: null,
        userId: null,
        bankId: null,
        currentQuestionId: null,
        currentSortOrder: null,
        totalQuestions: null,
        completedQuestions: null,
        correctQuestions: null,
        progressPercent: null,
        accuracyRate: null,
        lastPracticeTime: null,
        totalPracticeTime: null,
        isCompleted: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.progressId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户题库进度";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const progressId = row.progressId || this.ids
      getUserBankProgress(progressId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户题库进度";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.progressId != null) {
            updateUserBankProgress(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUserBankProgress(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const progressIds = row.progressId || this.ids;
      this.$modal.confirm('是否确认删除用户题库进度编号为"' + progressIds + '"的数据项？').then(function() {
        return delUserBankProgress(progressIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('exam/userBankProgress/export', {
        ...this.queryParams
      }, `userBankProgress_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
